/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.*;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.*;

/**
 *
 * @author HoaDinhGiang
 */
public class QuanLySanPhamInternalFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form QuanLySanPhamInternalFrame
     */
    private DefaultComboBoxModel comboBoxModelLoaiHang;
    private DefaultComboBoxModel comboBoxModelDonViTinh;
    private DefaultComboBoxModel comboBoxModelNhaSanXuat;
    private DefaultComboBoxModel comboBoxModelNhaPhanPhoi;
    private DefaultComboBoxModel comboBoxModelKhoHang;
    private List<Hanghoa> dsHangHoa;
    private int _maHangHoa;
    private Hanghoa _TeampObjectHangHoa;
    private DefaultTableModel dtm;

    public QuanLySanPhamInternalFrame() {
        initComponents();
        dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Tên Sản Phẩm");
        dtm.addColumn("Loại Sản Phẩm");
        dtm.addColumn("Đơn Vị");
        dtm.addColumn("Giá Bán");
        dtm.addColumn("Giá Tối Thiểu");
        dtm.addColumn("Giá Tối Đa");
        dtm.addColumn("Nhà Phân Phối");
        dtm.addColumn("Nhà Sản Xuất");
        dtm.addColumn("Kho");
        dtm.addColumn("Hàng Tồn");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jtfMaHang = new javax.swing.JTextField();
        jtfGiaToiThieu = new javax.swing.JTextField();
        jtfGiaBan1 = new javax.swing.JTextField();
        jtfGiaToiDa = new javax.swing.JTextField();
        jtfHangTon = new javax.swing.JTextField();
        javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSachHangHoa = new javax.swing.JTable();
        jbtThoat = new javax.swing.JButton();
        jbtSua = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtThem = new javax.swing.JButton();
        jtfTenHang = new javax.swing.JTextField();
        jcbLoaiHang = new javax.swing.JComboBox();
        jcbDonVi = new javax.swing.JComboBox();
        jcbNhaSanXuat = new javax.swing.JComboBox();
        jcbKhoHang = new javax.swing.JComboBox();
        jcbNhaPhanPhoi = new javax.swing.JComboBox();
        jbnTimKiem = new javax.swing.JButton();
        jbtLoadAll = new javax.swing.JButton();
        jbtClear = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Quản Lý Sản Phẩm");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setText("Mã Hàng Hóa:");

        jLabel2.setText("Tên Hàng Hóa:");

        jLabel3.setText("Nhà Phân Phối:");

        jLabel4.setText("Nhà Sản Xuất:");

        jLabel5.setText("Đơn Vị:");

        jLabel6.setText("Giá Bán:");

        jLabel7.setText("Giá Tối Thiểu:");

        jLabel8.setText("Giá Tối Đa:");

        jLabel9.setText("Kho Hàng:");
        jLabel9.setToolTipText("");

        jLabel10.setText("Loại Hàng Hóa:");

        jLabel11.setText("Hàng Tồn:");

        jtfMaHang.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jtfHangTon.setEnabled(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Sản Phẩm"));

        jtbDanhSachHangHoa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbDanhSachHangHoa.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jtbDanhSachHangHoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbDanhSachHangHoaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbDanhSachHangHoa);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        jbtSua.setText("Cập Nhật");
        jbtSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSuaActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbnTimKiem.setText("Tìm Kiếm");
        jbnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbnTimKiemActionPerformed(evt);
            }
        });

        jbtLoadAll.setText("Tải Lại");
        jbtLoadAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLoadAllActionPerformed(evt);
            }
        });

        jbtClear.setText("Clear All");
        jbtClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(jcbNhaSanXuat, 0, 144, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel9))
                                        .addGap(37, 37, 37)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jcbKhoHang, 0, 144, Short.MAX_VALUE)
                                            .addComponent(jtfGiaBan1)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)
                                        .addComponent(jtfMaHang)))
                                .addGap(69, 69, 69)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel7))
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jtfTenHang, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfGiaToiThieu, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcbNhaPhanPhoi, javax.swing.GroupLayout.Alignment.LEADING, 0, 154, Short.MAX_VALUE)
                                    .addComponent(jtfHangTon))
                                .addGap(79, 79, 79)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel10)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel8))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jcbLoaiHang, 0, 157, Short.MAX_VALUE)
                                            .addComponent(jcbDonVi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jtfGiaToiDa)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jbnTimKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                                        .addComponent(jbtLoadAll)))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbtClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtThem, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jbtXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jbtSua, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(151, 151, 151)
                        .addComponent(jbtThoat)
                        .addGap(30, 30, 30))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jtfMaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jtfTenHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbLoaiHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jcbDonVi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbNhaSanXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbNhaPhanPhoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfGiaBan1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jtfGiaToiThieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jtfGiaToiDa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel11)
                    .addComponent(jtfHangTon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbKhoHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbnTimKiem)
                    .addComponent(jbtLoadAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(jbtSua, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbtXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbtThem, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbtThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtClear))
                .addGap(59, 59, 59))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean KiemTraThongTinNhap() {
        boolean flag = true;
        if (jtfTenHang.getText().length() == 0 || jtfGiaBan1.getText().length() == 0
                || jtfGiaToiThieu.getText().length() == 0 || jtfGiaToiDa.getText().length() == 0
                || jtfHangTon.getText().length() == 0) {
            return false;
        }
        return flag;
    }

    public void loadcomboxLoaiHang() {
        try {
            this.comboBoxModelLoaiHang = new DefaultComboBoxModel();
            this.jcbLoaiHang.setModel(comboBoxModelLoaiHang);
            List<Loaihanghoa> ds = loaihangDAO.layDanhSachLoaiHang();
            for (int i = 0; i < ds.size(); i++) {
                Loaihanghoa dm = ds.get(i);
                this.comboBoxModelLoaiHang.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public int indexCbLoaiHangTuTable(Hanghoa hh) {
        int ix = 0;
        for (int i = 0; i < this.jcbLoaiHang.getItemCount(); i++) {
            Loaihanghoa lh = (Loaihanghoa) jcbLoaiHang.getItemAt(i);
            if (lh.getMaloaihanghoa() == hh.getLoaihanghoa().getMaloaihanghoa()) {
                return i;
            }
        }
        return ix;
    }

    public void loadcomboxNhaPhanPhoi() {
        try {
            this.comboBoxModelNhaPhanPhoi = new DefaultComboBoxModel();
            this.jcbNhaPhanPhoi.setModel(comboBoxModelNhaPhanPhoi);
            List<Nhaphanphoi> ds = nhaphanphoiDAO.layDanhSachNhaPhanPhoi();
            for (int i = 0; i < ds.size(); i++) {
                Nhaphanphoi dm = ds.get(i);
                this.comboBoxModelNhaPhanPhoi.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public int indexCbNhaPhanPhoiTuTable(Hanghoa hh) {
        int ix = 0;
        for (int i = 0; i < this.jcbNhaPhanPhoi.getItemCount(); i++) {
            Nhaphanphoi npp = (Nhaphanphoi) jcbNhaPhanPhoi.getItemAt(i);
            if (npp.getManhaphanphoi() == hh.getNhaphanphoi().getManhaphanphoi()) {
                return i;
            }
        }
        return ix;
    }

    public void loadcomboxKhoHang() {
        try {
            this.comboBoxModelKhoHang = new DefaultComboBoxModel();
            this.jcbKhoHang.setModel(comboBoxModelKhoHang);
            List<Khohang> ds = khohangDAO.layDanhSachKhoHang();
            for (int i = 0; i < ds.size(); i++) {
                Khohang dm = ds.get(i);
                this.comboBoxModelKhoHang.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public int indexCbKhoHangTuTable(Hanghoa hh) {
        int ix = 0;
        for (int i = 0; i < this.jcbKhoHang.getItemCount(); i++) {
            Khohang kh = (Khohang) jcbKhoHang.getItemAt(i);
            if (kh.getMakhohang() == hh.getKhohang().getMakhohang()) {
                return i;
            }
        }
        return ix;
    }

    public void loadcomboxDonViTinh() {
        try {
            this.comboBoxModelDonViTinh = new DefaultComboBoxModel();
            this.jcbDonVi.setModel(comboBoxModelDonViTinh);
            List<Donvitinh> ds = donviDAO.layDanhSachDonViTinh();
            for (int i = 0; i < ds.size(); i++) {
                Donvitinh dm = ds.get(i);
                this.comboBoxModelDonViTinh.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public int indexCbDonViTinhTable(Hanghoa hh) {
        int ix = 0;
        for (int i = 0; i < this.jcbDonVi.getItemCount(); i++) {
            Donvitinh dv = (Donvitinh) jcbDonVi.getItemAt(i);
            if (dv.getMadonvitinh() == hh.getDonvitinh().getMadonvitinh()) {
                return i;
            }
        }
        return ix;
    }

    public void loadcomboxNhaSanXuat() {
        try {
            this.comboBoxModelNhaSanXuat = new DefaultComboBoxModel();
            this.jcbNhaSanXuat.setModel(comboBoxModelNhaSanXuat);
            List<Nhasanxuat> ds = nhasanxuatDAO.layDanhSachNhaSanXuat();
            for (int i = 0; i < ds.size(); i++) {
                Nhasanxuat dm = ds.get(i);
                this.comboBoxModelNhaSanXuat.addElement(dm);
            }
        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public int indexCbNhaSanXuatTable(Hanghoa hh) {
        int ix = 0;
        for (int i = 0; i < this.jcbNhaSanXuat.getItemCount(); i++) {
            Nhasanxuat nsx = (Nhasanxuat) jcbNhaSanXuat.getItemAt(i);
            if (nsx.getManhasanxuat() == hh.getNhasanxuat().getManhasanxuat()) {
                return i;
            }
        }
        return ix;
    }

    public void hienThiHangHoa() {

        jtbDanhSachHangHoa.removeAll();
        dsHangHoa = hanghoaDAO.layDanhSachHangHoaTonTai();
        //Tạo model cho Table


        dtm.setNumRows(dsHangHoa.size());
        for (int i = 0; i < dsHangHoa.size(); i++) {
            Hanghoa hh = dsHangHoa.get(i);
            dtm.setValueAt(hh.getMahanghoa(), i, 0);
            dtm.setValueAt(hh.getTenhanghoa(), i, 1);
            dtm.setValueAt(hh.getLoaihanghoa().getTenloaihanghoa(), i, 2);
            dtm.setValueAt(hh.getDonvitinh().getTendonvitinh(), i, 3);
            dtm.setValueAt(hh.getGiaban(), i, 4);
            dtm.setValueAt(hh.getGiatoithieu(), i, 5);
            dtm.setValueAt(hh.getGiatoida(), i, 6);
            dtm.setValueAt(hh.getNhaphanphoi().getTennhaphanphoi(), i, 7);
            dtm.setValueAt(hh.getNhasanxuat().getTennhasanxuat(), i, 8);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 9);
            dtm.setValueAt(hh.getHangton(), i, 10);
        }
        //Hiển thị lên Table
        this.jtbDanhSachHangHoa.setModel(dtm);
        this.jtbDanhSachHangHoa.changeSelection(0, 0, false, false);

    }

    public boolean hienThiTimKiem() {

        dsHangHoa = hanghoaDAO.timKiemHangHoa(_TeampObjectHangHoa);
        if (dsHangHoa.isEmpty()) {
            return false;
        }
        jtbDanhSachHangHoa.removeAll();

        dtm.setNumRows(dsHangHoa.size());
        for (int i = 0; i < dsHangHoa.size(); i++) {
            Hanghoa hh = dsHangHoa.get(i);
            dtm.setValueAt(hh.getMahanghoa(), i, 0);
            dtm.setValueAt(hh.getTenhanghoa(), i, 1);
            dtm.setValueAt(hh.getLoaihanghoa().getTenloaihanghoa(), i, 2);
            dtm.setValueAt(hh.getDonvitinh().getTendonvitinh(), i, 3);
            dtm.setValueAt(hh.getGiaban(), i, 4);
            dtm.setValueAt(hh.getGiatoithieu(), i, 5);
            dtm.setValueAt(hh.getGiatoida(), i, 6);
            dtm.setValueAt(hh.getNhaphanphoi().getTennhaphanphoi(), i, 7);
            dtm.setValueAt(hh.getNhasanxuat().getTennhasanxuat(), i, 8);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 9);
            dtm.setValueAt(hh.getHangton(), i, 10);
        }
        //Hiển thị lên Table
        this.jtbDanhSachHangHoa.setModel(dtm);
        this.jtbDanhSachHangHoa.changeSelection(0, 0, false, false);
        return true;
    }
    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSuaActionPerformed
        // TODO add your handling code here:
        if (KiemTraThongTinNhap() == false) {
            JOptionPane.showMessageDialog(this, "Chưa Nhập Đủ Thông Tin.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
            return;
        }
        //mã hàng là mã được lưu trong biến _maHangHoa khi click chuột chọn sản phẩm
        int maHang = _maHangHoa;
        Hanghoa hh = hanghoaDAO.layThongTinHangHoa(maHang);

        if (hh != null) {
            hh.setTenhanghoa(jtfTenHang.getText());
            Loaihanghoa lhh = (Loaihanghoa) jcbLoaiHang.getItemAt(jcbLoaiHang.getSelectedIndex());
            hh.setLoaihanghoa(lhh);
            Nhaphanphoi npp = (Nhaphanphoi) jcbNhaPhanPhoi.getItemAt(jcbNhaPhanPhoi.getSelectedIndex());
            hh.setNhaphanphoi(npp);
            Nhasanxuat nsx = (Nhasanxuat) jcbNhaSanXuat.getItemAt(jcbNhaSanXuat.getSelectedIndex());
            hh.setNhasanxuat(nsx);
            Donvitinh dvt = (Donvitinh) jcbDonVi.getItemAt(jcbDonVi.getSelectedIndex());
            hh.setDonvitinh(dvt);
            hh.setGiaban(Double.parseDouble(jtfGiaBan1.getText()));
            hh.setGiatoithieu(Double.parseDouble(jtfGiaToiThieu.getText()));
            hh.setGiatoida(Double.parseDouble(jtfGiaToiDa.getText()));
            Khohang kh = (Khohang) jcbKhoHang.getItemAt(jcbKhoHang.getSelectedIndex());
            hh.setKhohang(kh);
          //  hh.setHangton(0);
            boolean kq = hanghoaDAO.capNhatThongTinHangHoa(hh);
            if (kq == true) {
                hienThiHangHoa();
                JOptionPane.showMessageDialog(this, "Cập Nhật cửa hàng thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Cập Nhật cửa hàng thất bại");
            }
        }
    }//GEN-LAST:event_jbtSuaActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
        int maHang = Integer.parseInt(jtfMaHang.getText());
        Hanghoa hh = hanghoaDAO.layThongTinHangHoa(maHang);

        if (hh != null) {
            hh.setKichhoat(false);
            boolean kq = hanghoaDAO.capNhatThongTinHangHoa(hh);
            if (kq == true) {
                hienThiHangHoa();
                JOptionPane.showMessageDialog(this, "Xóa sản phẩm thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Xóa sản phẩm thất bại");
            }
        }
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        if (KiemTraThongTinNhap() == false) {
            JOptionPane.showMessageDialog(this, "Chưa Nhập Đủ Thông Tin\nChỉ duy nhất mục Website có thể bỏ trống.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
            return;
        }

        Hanghoa hh = new Hanghoa();

        hh.setTenhanghoa(jtfTenHang.getText());
        Loaihanghoa lhh = (Loaihanghoa) jcbLoaiHang.getItemAt(jcbLoaiHang.getSelectedIndex());
        hh.setLoaihanghoa(lhh);
        Nhaphanphoi npp = (Nhaphanphoi) jcbNhaPhanPhoi.getItemAt(jcbNhaPhanPhoi.getSelectedIndex());
        hh.setNhaphanphoi(npp);
        Nhasanxuat nsx = (Nhasanxuat) jcbNhaSanXuat.getItemAt(jcbNhaSanXuat.getSelectedIndex());
        hh.setNhasanxuat(nsx);
        Donvitinh dvt = (Donvitinh) jcbDonVi.getItemAt(jcbDonVi.getSelectedIndex());
        hh.setDonvitinh(dvt);
        hh.setGiaban(Double.parseDouble(jtfGiaBan1.getText()));
        hh.setGiatoithieu(Double.parseDouble(jtfGiaToiThieu.getText()));
        hh.setGiatoida(Double.parseDouble(jtfGiaToiDa.getText()));
        Khohang kh = (Khohang) jcbKhoHang.getItemAt(jcbKhoHang.getSelectedIndex());
        hh.setKhohang(kh);
        hh.setHangton(0);
        hh.setKichhoat(true);
        boolean kq = hanghoaDAO.themHangHoa(hh);
        if (kq == true) {
            hienThiHangHoa();
            JOptionPane.showMessageDialog(this, "Thêm sản phẩm mới thành công");
        } else {
            JOptionPane.showMessageDialog(this, "Thêm sản phẩm mới thất bại");
        }
    }//GEN-LAST:event_jbtThemActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        loadcomboxLoaiHang();
        loadcomboxDonViTinh();
        loadcomboxNhaSanXuat();
        loadcomboxKhoHang();
        loadcomboxNhaPhanPhoi();
        hienThiHangHoa();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtbDanhSachHangHoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbDanhSachHangHoaMouseClicked
        // TODO add your handling code here:

        int dong = this.jtbDanhSachHangHoa.getSelectedRow();
        Hanghoa hh = dsHangHoa.get(dong);

        this.jtfMaHang.setText(String.valueOf(hh.getMahanghoa()));
        //lưu Mã hàng được chọn cho biến _maHangHoa để cập nhật sau này
        //Như vậy textfeld mã hàng sẽ dùng cho chức năng tìm kiếm
        _maHangHoa = hh.getMahanghoa();
        this.jtfTenHang.setText(hh.getTenhanghoa());
        this.jtfGiaBan1.setText(String.valueOf(hh.getGiaban()));
        this.jtfGiaToiThieu.setText(String.valueOf(hh.getGiatoithieu()));
        this.jtfGiaToiDa.setText(String.valueOf(hh.getGiatoida()));
        this.jtfHangTon.setText(String.valueOf(hh.getHangton()));
        jcbLoaiHang.setSelectedIndex(indexCbLoaiHangTuTable(hh));
        jcbNhaPhanPhoi.setSelectedIndex(indexCbNhaPhanPhoiTuTable(hh));
        jcbKhoHang.setSelectedIndex(indexCbKhoHangTuTable(hh));
        jcbDonVi.setSelectedIndex(indexCbDonViTinhTable(hh));
        jcbNhaSanXuat.setSelectedIndex(indexCbNhaSanXuatTable(hh));
    }//GEN-LAST:event_jtbDanhSachHangHoaMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void jbnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbnTimKiemActionPerformed
        // TODO add your handling code here:

        //gan cac gia tri da chon cho _TeampOb....

        _TeampObjectHangHoa = new Hanghoa();
        if (jtfMaHang.getText().length() == 0) {
            _TeampObjectHangHoa.setMahanghoa(-1);
        } else {
            _TeampObjectHangHoa.setMahanghoa(Integer.parseInt(jtfMaHang.getText()));
        }
        //    _TeampObjectHangHoa.setMahanghoa(11);

        if (jtfTenHang.getText().length() == 0) {
            _TeampObjectHangHoa.setTenhanghoa("");
        } else {
            _TeampObjectHangHoa.setTenhanghoa(jtfTenHang.getText());
        }


        Loaihanghoa lhh = (Loaihanghoa) jcbLoaiHang.getItemAt(jcbLoaiHang.getSelectedIndex());
        _TeampObjectHangHoa.setLoaihanghoa(lhh);

        Nhaphanphoi npp = (Nhaphanphoi) jcbNhaPhanPhoi.getItemAt(jcbNhaPhanPhoi.getSelectedIndex());
        _TeampObjectHangHoa.setNhaphanphoi(npp);

        Nhasanxuat nsx = (Nhasanxuat) jcbNhaSanXuat.getItemAt(jcbNhaSanXuat.getSelectedIndex());
        _TeampObjectHangHoa.setNhasanxuat(nsx);

        Donvitinh dvt = (Donvitinh) jcbDonVi.getItemAt(jcbDonVi.getSelectedIndex());
        _TeampObjectHangHoa.setDonvitinh(dvt);

        Khohang kh = (Khohang) jcbKhoHang.getItemAt(jcbKhoHang.getSelectedIndex());
        _TeampObjectHangHoa.setKhohang(kh);

        if (jtfGiaBan1.getText().length() == 0) {
            _TeampObjectHangHoa.setGiaban(-1.0);
        } else {
            _TeampObjectHangHoa.setGiaban(Double.parseDouble(jtfGiaBan1.getText()));
        }
        if (jtfGiaToiThieu.getText().length() == 0) {
            _TeampObjectHangHoa.setGiatoithieu(-1.0);
        } else {
            _TeampObjectHangHoa.setGiatoithieu(Double.parseDouble(jtfGiaToiThieu.getText()));
        }

        if (jtfGiaToiDa.getText().length() == 0) {
            _TeampObjectHangHoa.setGiatoida(-1.0);
        } else {
            _TeampObjectHangHoa.setGiatoida(Double.parseDouble(jtfGiaToiDa.getText()));
        }

        if (jtfHangTon.getText().length() == 0) {
            _TeampObjectHangHoa.setHangton(-1);
        } else {
            _TeampObjectHangHoa.setHangton(Integer.parseInt(jtfHangTon.getText()));
        }

        _TeampObjectHangHoa.setKichhoat(true);

        if (hienThiTimKiem() == false) {
            JOptionPane.showMessageDialog(this, "Không có sản phẩm.");
        } else {
        }
        
        
    }//GEN-LAST:event_jbnTimKiemActionPerformed

    private void jbtLoadAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLoadAllActionPerformed
        // TODO add your handling code here:
        hienThiHangHoa();
    }//GEN-LAST:event_jbtLoadAllActionPerformed

    private void jbtClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtClearActionPerformed
        // TODO add your handling code here:
        jtfGiaBan1.setText("");
        jtfGiaToiDa.setText("");
        jtfGiaToiThieu.setText("");
        jtfHangTon.setText("");
        jtfTenHang.setText("");
        jtfMaHang.setText("");
    }//GEN-LAST:event_jbtClearActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbnTimKiem;
    private javax.swing.JButton jbtClear;
    private javax.swing.JButton jbtLoadAll;
    private javax.swing.JButton jbtSua;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JComboBox jcbDonVi;
    private javax.swing.JComboBox jcbKhoHang;
    private javax.swing.JComboBox jcbLoaiHang;
    private javax.swing.JComboBox jcbNhaPhanPhoi;
    private javax.swing.JComboBox jcbNhaSanXuat;
    private javax.swing.JTable jtbDanhSachHangHoa;
    private javax.swing.JTextField jtfGiaBan1;
    private javax.swing.JTextField jtfGiaToiDa;
    private javax.swing.JTextField jtfGiaToiThieu;
    private javax.swing.JTextField jtfHangTon;
    private javax.swing.JTextField jtfMaHang;
    private javax.swing.JTextField jtfTenHang;
    // End of variables declaration//GEN-END:variables

    void setExtendedState(int MAXIMIZED_BOTH) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
